package com.AAL.Action;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.interceptor.SessionAware;

import com.AAL.DAO.PaymentOfDealerDAO;
import com.AAL.Model.Account;
import com.AAL.Model.PaymentOfDealer;


import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

@SuppressWarnings("serial")
public class PaymentAction extends ActionSupport implements SessionAware {

	/*
	 * Cac bien can cho viec phan trang
	 */
	private int prePage;
	private int nextPage;
	private int totalPageOfPayment;
	private int currentPage;
	private int startPage;

	/*
	 * TIm kiem
	 */
	private Timestamp fromDate;
	private Timestamp toDate;

	/*
	 * 
	 */
	private List<PaymentOfDealer> lstPaymentOfDealers;
	private Account myAccount;

	private PaymentOfDealerDAO lnkPaymentOfDealerDAO;
	private Map<String, Object> session;

	public PaymentAction() {
		this.lnkPaymentOfDealerDAO = new PaymentOfDealerDAO();
	}

	/*
	 * Minh
	 */
	private List<PaymentOfDealer> pagingOfPaymeyOfDealer(
			List<PaymentOfDealer> lstAlDealers, int page) {
		List<PaymentOfDealer> lstTemp = new ArrayList<PaymentOfDealer>();
		int maxlength = lstAlDealers.size();
		int numRecordInPage = 5;
		for (int i = 0; i < numRecordInPage; i++) {
			int temp = numRecordInPage * (page - 1) + i;
			if (temp < maxlength) {
				lstTemp.add(lstAlDealers.get(temp));
			} else {
				break;
			}
		}
		return lstTemp;
	}

	// quan ly viec thanh toan cua dealer
	public String paymentOfDealer() {
		try {
			if (session.get("myAccount") != null) {
				myAccount = (Account) session.get("myAccount");

				session.remove("myPaymentOfDealer");
				session.remove("totalPageOfPayment");
				session.remove("p_i");
				session.remove("startPage");
				List<PaymentOfDealer> lstAllPaymentOfDealers = lnkPaymentOfDealerDAO
						.getAllLstPaymentOfDealersByDealer(myAccount
								.getDealer());

				session.put("myPaymentOfDealer", lstAllPaymentOfDealers);
				// lay so bang ghi de phan trang
				int totalRecordPaymentOfDealer = lstAllPaymentOfDealers.size();
				// hien thi 5 bang ghi tren moi trang
				this.totalPageOfPayment = 0;
				int numRecordInPage = 5;
				if (totalRecordPaymentOfDealer % numRecordInPage == 0) {
					this.totalPageOfPayment = totalRecordPaymentOfDealer
							/ numRecordInPage;
				} else {
					this.totalPageOfPayment = totalRecordPaymentOfDealer
							/ numRecordInPage + 1;
				}

				session.put("totalPageOfPayment", this.totalPageOfPayment);

				this.lstPaymentOfDealers = pagingOfPaymeyOfDealer(
						lstAllPaymentOfDealers, 1);
				paging(0);
			}
		} catch (Exception e) {
			addActionError("Loi: " + e.getMessage());
			return ERROR;
		}
		return SUCCESS;
	}

	private void paging(int page) {

		if (page == 0) {
			this.startPage = 1;
			this.prePage = -1;
			this.nextPage = 2; // set 2 luon cung dc, vi voi 1 page thi no
								// se ko hien thi phan trang ra.
			this.currentPage = 1;
			session.put("startPage", this.startPage);
		} else {
			this.startPage = (Integer) session.get("startPage");
			this.totalPageOfPayment = (Integer) session
					.get("totalPageOfPayment");
			this.currentPage = page;
			this.prePage = page - 1;
			if (this.prePage <= 0)
				this.prePage = -1;
			this.nextPage = page + 1;
			if (this.nextPage > totalPageOfPayment) {
				this.nextPage = -1;
			}

			if (this.currentPage - this.startPage == 4) {
				this.startPage += 2;

				if (this.startPage + 5 >= this.totalPageOfPayment) {
					this.startPage = this.totalPageOfPayment + 1 - 5;
				}
			} else if (this.currentPage - this.startPage == 0) {

				this.startPage -= 2;
				if (this.startPage <= 0) {
					this.startPage = 1;
				}
			}
			session.put("startPage", this.startPage);
		}
	}

	@SuppressWarnings("unchecked")
	public String pagingViewPaymentOfDealer() {
		HttpServletRequest request = (HttpServletRequest) ActionContext
				.getContext().get(ServletActionContext.HTTP_REQUEST);
		try {
			if (session.get("myAccount") != null
					&& session.get("myPaymentOfDealer") != null) {
				List<PaymentOfDealer> lstAllPaymentOfDealers = (List<PaymentOfDealer>) session
						.get("myPaymentOfDealer");
				// lay so bang ghi de phan trang
				int page = Integer.parseInt(request.getParameter("p"));
				this.lstPaymentOfDealers = pagingOfPaymeyOfDealer(
						lstAllPaymentOfDealers, page);
				/* Phan trang */
				paging(page);
			}
		} catch (Exception e) {
			addActionError("Loi:" + e.getMessage());
			return ERROR;
		}
		return SUCCESS;
	}

	public String searchingPaymentOfDealer() {
		/*HttpServletRequest request = (HttpServletRequest) ActionContext
				.getContext().get(ServletActionContext.HTTP_REQUEST);*/
		try {
			// Xoa du lieu cua trang truoc. >"<
			session.remove("totalPageOfPayment");
			/*
			 * Timestamp fromDate = DateUtil.convertStringToTimestamp(request
			 * .getParameter("fromDate")); Timestamp toDate =
			 * DateUtil.convertStringToTimestamp(request
			 * .getParameter("toDate"));
			 */
			myAccount = (Account) session.get("myAccount");

			/*
			 * List<PaymentOfDealer> lstPaymentOfDealerTemp =
			 * this.lnkPaymentOfDealerDAO
			 * .getLstPaymentOfDealerByDealer(myAccount.getDealer(), fromDate,
			 * toDate);
			 * 
			 * session.put("searchingPaymentOfDealer", lstPaymentOfDealerTemp);
			 */
			/*
			 * this.lstPaymentOfDealers = this.lnkPaymentOfDealerDAO
			 * .getLstPaymentOfDealerByDealer(myAccount.getDealer(),
			 * DateUtil.formatTimestamp(fromDate),
			 * DateUtil.formatTimestamp(toDate));
			 */
			if (fromDate == null || toDate == null) {
				addActionMessage("Ban chua chi dinh ngay tim kiem.");
				return INPUT;
			} else {
				if (toDate.getTime() - fromDate.getTime() < 0) {
					addActionMessage("Ban chi dinh ngay tim kiem khong hop le.");
				} else {
					this.lstPaymentOfDealers = this.lnkPaymentOfDealerDAO
							.getLstPaymentOfDealerByDealer(
									myAccount.getDealer(), fromDate, toDate);
				}
			}

		} catch (Exception e) {
			return ERROR;
		}
		return SUCCESS;
	}

	/*
	 * ---------------------------------------------------------- Ket thuc Minh
	 * ----------------------------------------------------------
	 */

	@Override
	public void setSession(Map<String, Object> arg0) {
		this.session = arg0;
	}

	public List<PaymentOfDealer> getLstPaymentOfDealers() {
		return lstPaymentOfDealers;
	}

	public void setLstPaymentOfDealers(List<PaymentOfDealer> lstPaymentOfDealers) {
		this.lstPaymentOfDealers = lstPaymentOfDealers;
	}

	public int getPrePage() {
		return prePage;
	}

	public void setPrePage(int prePage) {
		this.prePage = prePage;
	}

	public int getNextPage() {
		return nextPage;
	}

	public void setNextPage(int nextPage) {
		this.nextPage = nextPage;
	}

	public int getCurrentPage() {
		return currentPage;
	}

	public void setCurrentPage(int currentPage) {
		this.currentPage = currentPage;
	}

	public int getStartPage() {
		return startPage;
	}

	public void setStartPage(int startPage) {
		this.startPage = startPage;
	}

	public Map<String, Object> getSession() {
		return session;
	}

	public void setTotalPageOfPayment(int totalPageOfPayment) {
		this.totalPageOfPayment = totalPageOfPayment;
	}

	public int getTotalPageOfPayment() {
		return totalPageOfPayment;
	}

	public Timestamp getToDate() {
		return toDate;
	}

	public void setToDate(Timestamp toDate) {
		this.toDate = toDate;
	}

	public Timestamp getFromDate() {
		return fromDate;
	}

	public void setFromDate(Timestamp fromDate) {
		this.fromDate = fromDate;
	}

}
